//
//  TakePhotoMgr.m
//  carcareIOS
//
//  Created by 王然 on 16/7/27.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "TakePhotoMgr.h"
#import "TakePhotoActionSheetV.h"
#import "SystemHelper.h"
#import "NetPictureMgr.h"
@interface TakePhotoMgr()< UIImagePickerControllerDelegate , UINavigationControllerDelegate , TakePhotoActionSheetVDelegate >

@property (nonatomic , strong) UIViewController * viewController;

@property (nonatomic , copy) CallBack callBack;

//用于保存本身的图片
@property (nonatomic , strong) UIImage * saveImage;

//用于保存本身图片的的data
@property (nonatomic , copy) NSData * saveImageData;


@end
@implementation TakePhotoMgr
IMP_SINGLETON

- (void)selectActionPictureWithController:(UIViewController *)viewController callBack:(CallBack)backImageBlock
{
    self.viewController = viewController;
    
    TakePhotoActionSheetV * takeV = [[TakePhotoActionSheetV alloc]initWithFuncBtnArr:@[LOCSTRING(@"menu_camera"),LOCSTRING(@"menu_pic")]];
    takeV.delegate = self;
    UIWindow *mainWindow = [UIApplication sharedApplication].delegate.window;
//    NSArray * windows = [[UIApplication sharedApplication]windows];
//    UIWindow * lastWindow = (UIWindow *)[windows lastObject];
//    mainWindow.windowLevel = lastWindow.windowLevel + 1;
    [UIView animateWithDuration:0.5 animations:^{
        [mainWindow addSubview:takeV];
    }];
    
    
    self.callBack = backImageBlock;
}
- (void)actionSheetBtnClick:(NSInteger)index
{
    switch (index) {
        case 0:
        {
            
            if (!SYSTEMHELPER.isOpenMedia) {
//                [[[UIAlertView alloc]initWithTitle:LOCSTRING(@"tips_media_no_open") message:[NSString stringWithFormat:@"%@\n%@",LOCSTRING(@"tips_media_open_1"),LOCSTRING(@"tips_media_open_2")] delegate:self cancelButtonTitle:LOCSTRING(@"ok") otherButtonTitles: nil]show];
                
                TipsView *tips = [TipsView textTipsWithTitle:LOCSTRING(@"tips_media_no_open") lightTitles:@[LOCSTRING(@"tips_media_open_1"),LOCSTRING(@"tips_media_open_2")]];
                [tips addOKButton:LOCSTRING(@"ok") clickOK:^{
                    [AlertMgr dismiss:TYPE_VIEW_ALERT];
                }];
                [AlertMgr showView:tips];
            }else{
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                picker.delegate = self;
                picker.allowsEditing = YES;
                //            self.viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
                //            self.viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
                [self.viewController presentViewController:picker animated:YES completion:nil];
            }
        }
            break;
            
        case 1:
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            picker.delegate = self;
            [self.viewController presentViewController:picker animated:YES completion:nil];
        }
            break;
            
        default:
            break;
    }

}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //
    UIImage *oldImage = (picker.sourceType == UIImagePickerControllerSourceTypeCamera ? info[UIImagePickerControllerEditedImage] : info[UIImagePickerControllerOriginalImage]);
    
    CGRect rect = [info[@"UIImagePickerControllerCropRect"] CGRectValue];
    //    CGFloat length = oldImage.size.width * 9 / 10;
    //    CGFloat imageH = 251 * __MAIN_RATIO_375;
    oldImage = [ImageHelper cutImage:oldImage rect:CGRectMake( rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
    
    self.saveImage = [ImageHelper watermarkImage:oldImage withText:@"图片仅供车挣审核使用"];
    
    if(self.callBack) self.callBack(self.saveImage);
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)uploadPictureType:(NSString *)pictureType withNetSuccess:(void (^)(id JSON))success fail:(void (^)(id JSON))fail
{
    if (self.saveImage) {
        self.saveImageData = [ImageHelper dataWithImage:self.saveImage compressLength:2000];
        NSInteger uid = USERINFO.userID;
        NSString * key = USERINFO.iczkey;
        
        [[NetPictureMgr uploadPictureWithDic:@{ @"icz_uid" : @(uid) , @"icz_key": key ,@"icz_imgType": pictureType , @"icz_fromType" : @"IOS" , @"file" : self.saveImageData}]requestSuccess:^(id JSON) {
            if(success) success(JSON);
        } failure:^(id JSON) {
            if(fail) fail(JSON);
        }];
        
    }else
    {
        [AlertMgr showShortWarnTextTips:@[@"请先选择上传图片哦!"]];
    }
}
@end
